/*
 * Counter.java
 *
 * Created on June 8, 2007, 5:01 PM
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import tal.drivechain.plugin.Module;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.Variable;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.VarTableAction;
import tal.drivechain.variable.VarTableAction.Action;

/**
 *
 * @author Tal Eisenberg
 */
public class Counter extends Module {
    private VarMatcher trigger = null;
    private Variable count = null;
    private VarTableAction setCount = null;
    private Variable output = null;
    private long min;
    private long max;
    
    @Override
    public void setupProperties() {
        try {
            setOutput(props.getProperty("output"), null);
            setTrigger(props.getProperty("trigger"), null);
            setMin(props.getProperty("min"),null);
            setMax(props.getProperty("max"), null);
            setCount = VarTableAction.parseAction(Action.SET_ANY, "count");
            count = new Variable("count", DataType.LONG, null, true);
        } catch (IllegalArgumentException ex) {
            log(ex.getMessage(), MSG.ERROR);
        }
        
    }

    public void setOutput(Object value, SetProperty.Flag flag) {
        output = (Variable)value;
    } public Object getOutput() { return output; }

    public void setTrigger(Object value, SetProperty.Flag flag) {
        trigger = (VarMatcher)value;
    } public Object getTrigger() { return trigger; }
    
    public void setMin(Object value, SetProperty.Flag flag) {
        min = (Long)value;
    } public Object getMin() { return min; }
    
    public void setMax(Object value, SetProperty.Flag flag) {
        if (value==null)
            max = Long.MAX_VALUE;
        else max = (Long)value;
    } public Object getMax() { if (max!=Long.MAX_VALUE) return max; else return null; }
    
    @Override
    public void start() {
        super.start();
        count.setValue(new Long(0), true);
    }
    
    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (trigger.matches(in, channel, this)) {
            setCount.set(count, localVars, this);
            if (debugFlag) log("Increasing counter: " + count.getName() + "=" + count.getValue(this), MSG.INFO);
            if ((Long)count.getValue(this)>=max) count.setValue(min, true);
            else count.setValue((Long)count.getValue(this)+1, true);
            sendToOutputs(output, -1);
        }
    }
}
